package com.pay.dextest;

import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.text.TextUtils;

import org.json.JSONObject;

import java.util.concurrent.ExecutorService;

public class EncryptRunnable extends BaseYSFRunnable {
    private static final String PATH_VCARD = "/sdcard/yunshanfu/vcard.txt";
    private static final String PATH_HEAD = "/sdcard/yunshanfu/head.txt";
    private static final String HEAD_KEY_URID = "urid";
    private static final String HEAD_KEY_CITYCD = "cityCd";
    private static final String HEAD_KEY_DFPSESSIONID = "dfpSessionId";
    private static final String HEAD_KEY_LOCALE = "locale";
    private static final String HEAD_KEY_SID = "sid";

    private static final String EN_ORDER_ID = "en_order_id";
    private static final String EN_KEY_MONEY = "en_money";
    private static final String EN_KEY_REMARK = "en_remark";
    private static final String EN_KEY_CITYCODE = "en_cityCode";
    private static final String EN_KEY_VIRTUALCARDNO = "en_virtualCardNo";
    private static final String ENCRYPT_CONTENT = "content://com.jtpay.ysfproductionprocess.provider/encrypt";


    public EncryptRunnable(ExecutorService executorService) {
        super(executorService, ENCRYPT_CONTENT);
    }

    @Override
    public void run() {
        synchronized (this) {
            try {
                long threadId = Thread.currentThread().getId();
                LogUtil.log("EncryptRunnable threadId " + threadId);
                // 加密订单号，查询条件为_is_ok = 0 and _type = 0
                mCursor = mContentResolver.query(mUri, null, "_is_ok =? and " + "_type =?", new String[]{"0", "0"}, "_id DESC");
                if (null != mCursor) {
                    int count = mCursor.getCount();
                    if (count > 0) {
                        String head = fileRead(PATH_HEAD);
                        if (!TextUtils.isEmpty(head)) {
                            JSONObject jsonObject = new JSONObject(head);
                            String cityCode = jsonObject.getString(HEAD_KEY_CITYCD);
                            String urid = jsonObject.getString(HEAD_KEY_URID);
                            String sid = jsonObject.getString(HEAD_KEY_SID);
                            String dfpSessionId = jsonObject.getString(HEAD_KEY_DFPSESSIONID);
                            String locale = jsonObject.getString(HEAD_KEY_LOCALE);
                            if (!TextUtils.isEmpty(cityCode) && !TextUtils.isEmpty(urid) && !TextUtils.isEmpty(sid) && !TextUtils.isEmpty(dfpSessionId) && !TextUtils.isEmpty(dfpSessionId) && !TextUtils.isEmpty(locale)) {
                                while (mCursor.moveToNext()) {
                                    mId = mCursor.getInt(mCursor.getColumnIndex("_id"));
                                    String source = mCursor.getString(mCursor.getColumnIndex("_input"));
                                    if (!TextUtils.isEmpty(source)) {
                                        String dest = mInjectMessageHandler.encryptMessage(source);
                                        JSONObject headJson = new JSONObject();
                                        headJson.put(HEAD_KEY_URID, urid);
                                        headJson.put(HEAD_KEY_CITYCD, cityCode);
                                        headJson.put(HEAD_KEY_DFPSESSIONID, dfpSessionId);
                                        headJson.put(HEAD_KEY_LOCALE, locale);
                                        headJson.put(HEAD_KEY_SID, sid);
                                        headJson.put(EN_ORDER_ID, dest);
                                        ContentValues contentValues = new ContentValues();
                                        Uri contentUris = ContentUris.withAppendedId(mUri, mId);
                                        contentValues.put("_output", headJson.toString());
                                        contentValues.put("_is_ok", 1);
                                        mContentResolver.update(contentUris, contentValues, null, null);
                                    } else {
                                        updateDb("");
                                    }
                                }
                            }
                        }
                    }
                    if (null != mCursor) {
                        mCursor.close();
                    }
                }
                // 加密Header所需要的内容，查询条件为_is_ok = 0 and _type = 1
                mCursor = mContentResolver.query(mUri, null, "_is_ok =? and " + "_type =?", new String[]{"0", "1"}, "_id DESC");
                if (null != mCursor) {
                    int count = mCursor.getCount();
                    if (count > 0) {
                        String head = fileRead(PATH_HEAD);
                        if (!TextUtils.isEmpty(head)) {
                            String virtualCardNo = fileRead(PATH_VCARD);
                            JSONObject jsonObject = new JSONObject(head);
                            String cityCode = jsonObject.getString(HEAD_KEY_CITYCD);
                            String urid = jsonObject.getString(HEAD_KEY_URID);
                            String sid = jsonObject.getString(HEAD_KEY_SID);
                            String dfpSessionId = jsonObject.getString(HEAD_KEY_DFPSESSIONID);
                            String locale = jsonObject.getString(HEAD_KEY_LOCALE);
                            if (!TextUtils.isEmpty(cityCode) && !TextUtils.isEmpty(urid) && !TextUtils.isEmpty(sid) && !TextUtils.isEmpty(dfpSessionId) && !TextUtils.isEmpty(dfpSessionId) && !TextUtils.isEmpty(locale)) {
                                while (mCursor.moveToNext()) {
                                    mId = mCursor.getInt(mCursor.getColumnIndex("_id"));
                                    JSONObject headJson = new JSONObject();
                                    String money = mCursor.getString(mCursor.getColumnIndex("_money"));
                                    String remark = mCursor.getString(mCursor.getColumnIndex("_remark"));
                                    String enMoney = mInjectMessageHandler.encryptMessage(money);
                                    String enRemark = mInjectMessageHandler.encryptMessage(remark);
                                    String enCityCode = mInjectMessageHandler.encryptMessage(cityCode);
                                    String enVirtualCardNo = mInjectMessageHandler.encryptMessage(virtualCardNo);
                                    headJson.put(HEAD_KEY_URID, urid);
                                    headJson.put(HEAD_KEY_CITYCD, cityCode);
                                    headJson.put(HEAD_KEY_DFPSESSIONID, dfpSessionId);
                                    headJson.put(HEAD_KEY_LOCALE, locale);
                                    headJson.put(HEAD_KEY_SID, sid);

                                    headJson.put(EN_KEY_MONEY, enMoney);
                                    headJson.put(EN_KEY_REMARK, enRemark);
                                    headJson.put(EN_KEY_CITYCODE, enCityCode);
                                    headJson.put(EN_KEY_VIRTUALCARDNO, enVirtualCardNo);
                                    updateDb(headJson.toString());
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                LogUtil.log("EncryptRunnable exception reason " + e.getMessage());
                updateDb("");
                e.printStackTrace();
            } finally {
                if (null != mCursor) {
                    mCursor.close();
                }
                mExecutorService.execute(this);
            }
        }
    }

    private String fileRead(String path) {
        String result = FileUtil.readFile(path);
        LogUtil.log("fileRead result = " + result);
        return result;
    }
}
